# Perform data preparation & spatial analysis
# by Piatinskii M.

library("spatialEco")
library("sp")
library("spdep")
library("dplyr")

# load bio data
data.anchovy <- read.csv("input/data-anchovy-lampar.csv")
data.mnemiop <- read.csv("input/data-mnemiopsis.csv")
data.clup <- read.csv("input/data-clupionella-lampara.csv")
# load geo dictionary
data.grid <- read.csv("input/data-coordinates.csv")

data.anchovy$square <- tolower(data.anchovy$square)
data.mnemiop$square <- tolower(data.mnemiop$square)
data.clup$square <- tolower(data.clup$square)

# merge data & coordinates
data.anchovy <- merge(data.anchovy, data.grid, by.x = "square", by.y = "square_lower") %>%
  select(c("year", "month", "square", "catch", "lat", "lon")) %>%
  arrange(year, month)

data.clup <- merge(data.clup, data.grid, by.x = "square", by.y = "square_lower") %>%
  select(c("year", "month", "square", "catch", "lat", "lon")) %>%
  arrange(year, month)

data.mnemiop <- merge(data.mnemiop, data.grid, by.x = "square", by.y = "square_lower") %>%
  select(c("year", "month", "square", "concentration", "lat", "lon")) %>%
  arrange(year, month)

# preliminary statistics
stat.anchovy <- data.anchovy %>%
  group_by(year) %>%
  summarise(n = n())

stat.clup <- data.clup %>%
  group_by(year) %>%
  summarise(n = n())

stat.mnem <- data.mnemiop %>%
  group_by(year) %>%
  summarise(n = n())

stat.df <- merge(stat.anchovy, stat.clup, by.x = "year", by.y = "year")
stat.df <- merge(stat.df, stat.mnem, by.x = "year", by.y = "year")
names(stat.df) <- c("year", "anchovy", "clup", "mnem")
print("=========== Preliminary statistics")
print(stat.df)


# transform to mercator object 
#crs.merc <- CRS("+init=epsg:3395")

coordinates(data.anchovy) <- ~lon+lat
proj4string(data.anchovy) <- CRS("+init=epsg:4326")

coordinates(data.clup) <- ~lon+lat
proj4string(data.clup) <- CRS("+init=epsg:4326")

coordinates(data.mnemiop) <- ~lon+lat
proj4string(data.mnemiop) <- CRS("+init=epsg:4326")

# convert to world mercator system
data.anchovy <- st_transform(st_as_sf(data.anchovy), 3395)
data.anchovy$x <- st_coordinates(data.anchovy)[,"X"]
data.anchovy$y <- st_coordinates(data.anchovy)[,"Y"]

data.mnemiop <- st_transform(st_as_sf(data.mnemiop), 3395)
data.mnemiop$x <- st_coordinates(data.mnemiop)[,"X"]
data.mnemiop$y <- st_coordinates(data.mnemiop)[,"Y"]

data.clup <- st_transform(st_as_sf(data.clup), 3395)
data.clup$x <- st_coordinates(data.clup)[,"X"]
data.clup$y <- st_coordinates(data.clup)[,"Y"]

# perform partial assessment 
part.anchovy <- data.anchovy[which(data.anchovy$year == 2008 & data.anchovy$month == 6),]
part.mnem <- data.mnemiop[which(data.mnemiop$year == 2008 & data.mnemiop$month == 6),]

sv.anchovy <- as.data.frame(part.anchovy) %>%
  select(x, y, catch)
names(sv.anchovy) <- c("x", "y", "row1") 

sv.mnem <- as.data.frame(part.mnem) %>%
  select(x, y, concentration)
names(sv.mnem) <- c("x", "y", "row2")

write.csv(sv.anchovy, file = "input/data-anchovy-lamp-2008-june.csv")
write.csv(sv.mnem, file = "input/data-mnem-2008-june.csv")

